package yx.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.CommonAddrbookServiceInt;
import yx.vo.AddrbookVO;
import yx.vo.DatagridVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "commonAddrbookAction")
public class CommonAddrbookAction extends BaseAction implements ModelDriven<AddrbookVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CommonAddrbookAction.class);

	private AddrbookVO addrbook = new AddrbookVO();

	@Override
	public AddrbookVO getModel() {
		return addrbook;
	}

	private CommonAddrbookServiceInt commonAddrbookService;

	public CommonAddrbookServiceInt getCommonAddrbookService() {
		return commonAddrbookService;
	}

	@Autowired
	public void setCommonAddrbookService(CommonAddrbookServiceInt commonAddrbookService) {
		this.commonAddrbookService = commonAddrbookService;
	}

	public void queryAddrbook() {
		super.writeJson(commonAddrbookService.datagrid(addrbook));
	}

	public void addAddrbook() {
		JsonVO j = new JsonVO();
		try {
			AddrbookVO u = commonAddrbookService.save(addrbook);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeAddrbook() {
		commonAddrbookService.remove(addrbook.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editAddrbook() {
		JsonVO j = new JsonVO();
		try {
			AddrbookVO u = commonAddrbookService.edit(addrbook);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
}
